package io.framework.modules.pollutionsource.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.framework.common.constant.Constant;
import io.framework.common.page.PageData;
import io.framework.common.service.impl.CrudServiceImpl;
import io.framework.common.utils.ConvertUtils;
import io.framework.modules.pollutionsource.dto.PollutionGoordInfoDTO;
import io.framework.modules.pollutionsource.dao.TBasPollutionDao;
import io.framework.modules.pollutionsource.dao.TBasPollutionlabeldirDao;
import io.framework.modules.pollutionsource.dto.TBasPollutionDTO;
import io.framework.modules.pollutionsource.entity.TBasPollutionEntity;
import io.framework.modules.pollutionsource.service.TBasPollutionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 污染源监控基本信息
 *
 * @author Gujq email
 * @since 1.0.0 2020-03-24
 */
@Service
public class TBasPollutionServiceImpl extends CrudServiceImpl<TBasPollutionDao, TBasPollutionEntity, TBasPollutionDTO> implements TBasPollutionService {

    @Autowired
    private TBasPollutionDao basPollutionDao;

    @Autowired
    private TBasPollutionlabeldirDao basPollutionlabeldirDao;

    @Override
    public QueryWrapper<TBasPollutionEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<TBasPollutionEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

    @Override
    public PageData<TBasPollutionDTO> pages(Map<String , Object> params){

        IPage<TBasPollutionEntity> pages = getPage(params, Constant.CREATE_TIME, false);

        List<TBasPollutionEntity> list = baseDao.getList(params);

        return getPageData(list,pages.getTotal(),TBasPollutionDTO.class);
    }

    public Map<String, Object> getParamsMap(Map<String, Object> params){
        String regionName[],attentionDegreeName[];
        if(params.containsKey("regionName")){
            String name= params.get("regionName").toString();
            if(StringUtils.isNotBlank(name)){
                regionName = name.split(",");
                params.put("regionName",regionName);
            }
        }
        if(params.containsKey("attentionDegreeName")){
            String name= params.get("attentionDegreeName").toString();
            if(StringUtils.isNotBlank(name)){
                attentionDegreeName = name.split(",");
                params.put("attentionDegreeName",attentionDegreeName);
            }
        }
        return params;
    }

    @Override
    public  List<TBasPollutionDTO> lists(Map<String, Object> params){

        params = getParamsMap(params);

        List<TBasPollutionEntity> list = baseDao.getList(params);
        return ConvertUtils.sourceToTarget(list,TBasPollutionDTO.class);
    }

    @Override
    public  List<TBasPollutionDTO> getListCoordNotNull(Map<String, Object> params){

        params = getParamsMap(params);

        List<TBasPollutionEntity> list = baseDao.getListCoordNotNull(params);
        return ConvertUtils.sourceToTarget(list,TBasPollutionDTO.class);
    }

    @Override
    public  List<PollutionGoordInfoDTO> getCoordList(Map<String, Object> params){

        params = getParamsMap(params);

        List<TBasPollutionEntity> list = baseDao.getCoordList(params);
        return ConvertUtils.sourceToTarget(list,PollutionGoordInfoDTO.class);
    }

    @Override
    public List<Map<String, Object>> getRegionCount(){
        List<Map<String, Object>> resultList = new ArrayList<>();
        List<Map<String,Object>> mapList1 = basPollutionDao.getRegionCount();
        List<Map<String,Object>> mapList2 = basPollutionlabeldirDao.getRegionCount();
        for(Map<String,Object> map1 : mapList1){
            Map<String, Object> result = new HashMap<String, Object>();
            String name1 =(String) map1.get("name");
            int count1 = ((Long)map1.get("count")).intValue();
            for(Map<String,Object> map2 : mapList2){
                String name2 =(String) map2.get("name");
                int count2 = ((Long)map2.get("count")).intValue();
                if(name2.equals(name1)){
                    count1 = count1 + count2;
                    break;
                }
            }
            result.put("name",name1);
            result.put("count",count1);
            resultList.add(result);
        }
        return resultList;
    }

    @Override
    public List<Map<String , Object>> getWryRegionTypeCount(){
        List<Map<String , Object>> mapResult = basPollutionlabeldirDao.getWryRegionTypeCount();
        mapResult.forEach(map ->{
            if("其他重点排污单位".equals(map.get("NAME"))){
                map.put("count",map.get("qtcount"));
            }
        });
        return mapResult;
    }

    @Override
    public List<Map<String, Object>> getWryTypeCount() {

        return baseDao.getWryTypeCount();
    }
}